package com.example.concurrentprogramming8016.thread.threadinterrupt;

import java.util.concurrent.TimeUnit;

/**
 * @author liyanhao
 * @Date 2023-02-08 0:01
 * 线程中断
 */
public class ThreadInterruptTest {
    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(() -> {
            while (true) {
                System.out.println("线程正在运行...");
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                if (Thread.currentThread().isInterrupted()) {
                    break;
                }
            }
        }, "t1");
        t1.start();
        //main线程休眠五秒后中断t1
        TimeUnit.SECONDS.sleep(5);
        t1.interrupt();
    }

}
